var COMMON_YELLOW = createColorWithRGBA(255, 153, 18, 255);
var COMMON_DARK = createColorWithRGBA(50, 51, 52, 255);
var COMMON_DARK_DEEP = createColorWithRGBA(40, 41, 42, 255);

var mainVC = new ViewController();
mainVC.view.backgroundColor = COMMON_DARK_DEEP;

var param = api_getPushedParam();

var projList = new List();
projList.addCallBackDidSelectRowAtIndexPath(function (section, row) {

	if (row == 0 || row >= projModels.length + 1) {
		return;
	}
	curShowProj = row - 1;
	pageCells = new Array();
	projModels[curShowProj].pages.forEach(page => {
		var cell = new projCell(page.pageName);
		pageCells.push(cell);
	});
	pageShowBaseViewList.reloadData(1, -1, -1, { "0": pageCells});
	displayPageShow(true);
});

var projModels = null;
var projCells = new Array();

function displayLabelS1() {
	Label.call(this);
	this.textAlignment = TextAlignmentCenter;
	this.textColor = whiteColor;
	this.fontSize = 35;
	this.backgroundColor = COMMON_DARK;
}

function projCell(content) {
	Cell.call(this);
	this.setCellFixHeight(50);

	var contentLabel = new displayLabelS1();
	contentLabel.textColor = COMMON_YELLOW;
	contentLabel.text = content;
	this.addSubView(contentLabel);
	contentLabel.vrt_layout.topEqualToView(null).leftEqualToView(null).heightIs(48).widthRatioToView(null, 1);

	var offset = new View();
	offset.backgroundColor = COMMON_YELLOW;
	this.addSubView(offset);
	offset.vrt_layout.topSpaceToView(contentLabel, 0).leftEqualToView(null).heightIs(2).widthRatioToView(null, 1);
}

var curShowProj = -1;
var pageShowBaseView;
var pageShowBaseViewList = new List();
var pageCells = new Array();
pageShowBaseViewList.addCallBackDidSelectRowAtIndexPath(function (section, row) {

	if(curShowProj == -1)
	{
		return;
	}

	if (row >= projModels[curShowProj].pages.length) {
		return;
	}

	var pageModel = projModels[curShowProj].pages[row];
	
	if(pageModel.pageCodeAddr.search("http") != -1)
	{
		api_pushUrlWithParam(pageModel.pageCodeAddr, null);
	}

	if(api_platform() == 'web_debugger')
	{
		api_webOnly_showPageCodeToEditor();
	}
});

const pageShowHeight = 800;
function displayPageShow(enable) {
	if (api_platform() == 'iOS') {
		api_dispatchWithAnimation(0.3, function () {
			if (enable) {
				pageShowBaseView._y = 0;
			}
			else {
				pageShowBaseView._y = -pageShowHeight;
			}
		});
	}
	else {
		if (enable) {
			pageShowBaseView._y = 0;
		}
		else {
			pageShowBaseView._y = -pageShowHeight;
		}
	}
}

var reqUrl = 'https://21xa689434.imwork.net:23333/vrt/getProjANDPages';
var req = new HttpRequest(reqUrl, function (data, info) {
	if (!data.isSuccess) {
		return;
	}

	var projArray = data.jsonData;
	if (!projArray) {
		return;
	}
	projModels = projArray;

	projCells = new Array();
	projCells.push(new projCell('All Projects'));
	projArray.forEach(projModel => {
		var cell = new projCell(projModel.projName);
		projCells.push(cell);
	});

	projList.reloadData(1, -1, -1, { "0": projCells });
});

mainVC.addCallBackViewDidLoad(function () {
	var refreshBtn = new displayLabelS1();
	refreshBtn.text = 'Refresh';
	refreshBtn.addClick(function () {
		req.request({ 'userToken': param.userToken });
	});
	mainVC.view.addSubView(refreshBtn);
	refreshBtn.vrt_layout.topEqualToView(null).leftEqualToView(null).heightIs(100).widthRatioToView(null, 1);

	mainVC.view.addSubView(projList);
	projList.vrt_layout.topSpaceToView(refreshBtn, 10).leftEqualToView(null).heightIs(mainVC.view.height() - 110).widthRatioToView(null, 1);


	pageShowBaseView = new View();
	pageShowBaseView.backgroundColor = COMMON_DARK;
	pageShowBaseView.cornerRadius = 5;
	mainVC.view.addSubView(pageShowBaseView);
	pageShowBaseView.vrt_layout.topSpaceToView(null, -pageShowHeight).leftEqualToView(null).heightIs(pageShowHeight).widthRatioToView(null, 1);
	
	var pageShowBaseViewColseBtn = new displayLabelS1();
	pageShowBaseViewColseBtn.text = 'Close';
	pageShowBaseView.addSubView(pageShowBaseViewColseBtn);
	pageShowBaseViewColseBtn.vrt_layout.topEqualToView(null).leftEqualToView(null).heightIs(100).widthRatioToView(null, 1);
	
	pageShowBaseViewColseBtn.addClick(function(){
		displayPageShow(false);
	});

	pageShowBaseView.addSubView(pageShowBaseViewList);
	pageShowBaseViewList.vrt_layout.topSpaceToView(pageShowBaseViewColseBtn, 10).leftEqualToView(null).heightIs(pageShowBaseView.height() - 110).widthRatioToView(null, 1);
	
	api_commitVC(mainVC);
});
